﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;

/// <summary>
///WGXX 的摘要说明
/// </summary>
public partial class WGXX
{
    public enum kindWG { 违规性质1 = 1, 违规性质2 = 2, 违规性质3 = 3, 违规性质4 = 4 }
    public enum typeWG { 违规类型1 = 1, 违规类型2 = 2, 违规类型3 = 3 }
    public enum typeUserWG { 普通用户 = 1, 教师, 机构 }
    public static WGXX GetByID(int id)
    {

        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.WGXX.FirstOrDefault(row => row.ID == id && row.Enable_Flag == 1);
        }
    }

    public static List<WGXX> GetAll()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            return context.WGXX.ToList();
        }
    }

    public void Add()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            context.WGXX.InsertOnSubmit(this);
            context.SubmitChanges();
        }
    }

    public void Update()
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            this.GetType().GetMethod("Detach").Invoke(this, null);
            var tbl = context.GetTable<WGXX>();
            tbl.Attach(this);
            context.Refresh(RefreshMode.KeepCurrentValues, this);
            context.SubmitChanges();
        }
    }

    public static void Delete(int id)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var objItem = context.WGXX.FirstOrDefault(row => row.ID == id);
            if (objItem == null)
                return;
            context.WGXX.DeleteOnSubmit(objItem);
            context.SubmitChanges();
        }
    }
    public static List<WGXX> GetAllInUserIDByConditon(int kindWG, int typeWG, string AddTimeForm, string AddTimeTo, int typeUserWG, int AddUserID, int AdminUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.WGXX
                        where t.Enable_Flag == 1 && t.RelationID == AddUserID
                        select t;
            if (kindWG != 0)
                query = query.Where(row => row.Kind == kindWG);
            if (typeWG != 0)
                query = query.Where(row => row.Type == typeWG);
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            if (typeUserWG != 0)
                query = query.Where(row => row.UserType == typeUserWG);

            if (AdminUserID != 0)
                query = query.Where(row => row.AdminUserID == AdminUserID);
            query = query.OrderByDescending(row => row.AddTime);
            return query.ToList();
        }
    }
    public static List<WGXX> GetAllByConditon(int kindWG, int typeWG, string AddTimeForm, string AddTimeTo, int typeUserWG, int AddUserID, int AdminUserID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            var query = from t in context.WGXX
                        where t.Enable_Flag == 1
                        select t;
            if (kindWG != 0)
                query = query.Where(row => row.Kind == kindWG);
            if (typeWG != 0)
                query = query.Where(row => row.Type == typeWG);
            if (!string.IsNullOrEmpty(AddTimeForm))
                query = query.Where(row => row.AddTime >= DongDong.Common.Misc.ConvertToDateTime(AddTimeForm));
            if (!string.IsNullOrEmpty(AddTimeTo))
                query = query.Where(row => row.AddTime <= DongDong.Common.Misc.ConvertToDateTime(AddTimeTo));
            if (typeUserWG != 0)
                query = query.Where(row => row.UserType == typeUserWG);
            if (AddUserID != 0)
                query = query.Where(row => row.RelationID == AddUserID);
            if (AdminUserID != 0)
                query = query.Where(row => row.AdminUserID == AdminUserID);
            query = query.OrderByDescending(row => row.AddTime);
            return query.ToList();
        }
    }

    public static void BadDelete(int ID)
    {
        using (DataClassesDataContext context = new DataClassesDataContext())
        {
            WGXX objWGXX = WGXX.GetByID(ID);
            if (objWGXX == null)
                return;
            objWGXX.Enable_Flag = 0;
            objWGXX.Update();
        }
    }
}